package com.fish.yssb.ui.page.activity.base;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.fish.yssb.app.BaseApp;
import com.fish.yssb.ui.page.activity.MainActivity;
import com.fish.yssb.ui.page.util.PrefeneceUtil;
import com.fish.yssb.ui.page.util.ScreenObserver;
import com.fish.yssb.ui.page.util.TimeoutService;
import com.fish.yssb.ui.page.util.ToastUtil;
import com.jaeger.library.StatusBarUtil;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Activity基类
 */
public abstract class BaseActivity extends FragmentActivity {
    public static final String KEY_PROJECTID = "project_id";

    protected View titlebar_back, titlebar_menu;
    protected TextView titlebar_title, titlebar_menu_text;


    private BaseApp app;
    private ScreenObserver mScreenObserver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (BaseApp)getApplication();
        startAD();
    }

    @Override
    protected void onResume() {
        startAD();
        super.onResume();

    }
    @Override
    protected void onStop() {
        handler.removeCallbacks(runnable);
        super.onStop();
    }



    public void showToast(String msg) {
        ToastUtil.getInstance(this).showShortToast(msg);
    }

    public void showToast(String msg, ToastUtil.TOAST_TYPE toast_type) {
        ToastUtil.getInstance(this).showToast(msg, toast_type);
    }


    private Handler handler = new Handler();
    private long time = 1000 * 60*5;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                handler.removeCallbacks(runnable);
                break;
            case MotionEvent.ACTION_UP:
                startAD();
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

            //调用系统API结束进程
            android.os.Process.killProcess(android.os.Process.myPid());

            //结束整个虚拟机进程，注意如果在manifest里用android:process给app指定了不止一个进程，则只会结束当前进程
            System.exit(0);
        }
    };

    public void startAD() {
       String ltime =  PrefeneceUtil.getData("para3_et_out_time","5",false);
       int iTime = Integer.parseInt(ltime);
        time = 1000*60*iTime;
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, time);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
